#include "CancelledErr.h"

#include <stdarg.h>
#include <string.h>
#include <fan-llvm/core/Core.h>
#include "Str.h"
#include "Type.h"


Type* CancelledErr_classType = NULL;
VTable* CancelledErr_classVTable = NULL;


/**********************************************
 *
 * Methods and fields
 *
 **********************************************/


/* Void make(Str msg, Err? cause) */
void CancelledErr_make_0(CancelledErr* self)
{
  CancelledErr_make(self, Str_defVal(), NULL);
}


/* Void make(Str msg, Err? cause) */
void CancelledErr_make_1(CancelledErr* self, Str* msg)
{
  CancelledErr_make(self, msg, NULL);
}


/* Void make(Str msg, Err? cause) */
void CancelledErr_make(CancelledErr* self, Str* msg, Err* cause)
{
  Err_make((Err*)self, msg, cause);
}


/**********************************************
 *
 * Extra methods
 *
 **********************************************/


/* Create a new CancelledErr instance and initialize it with CancelledErr_make() */
CancelledErr* CancelledErr_new(Str* msg, Err* cause)
{
  CancelledErr* err = (CancelledErr*)Obj_allocate(sizeof(CancelledErr), CancelledErr_classVTable);
  CancelledErr_make(err, msg, cause);
  return err;
}


/* Create a new CancelledErr instance and initialize it with the message given by the specified format.
 * 'msgFormat' uses the same format as Str_newWithFormat() */
CancelledErr* CancelledErr_newWithFormattedMsg(const char* msgFormat, ...)
{
  va_list args;
  va_start(args, msgFormat);
  Str* msg = Str_newWithFormatValist(msgFormat, args);
  va_end(args);
  return CancelledErr_new(msg, NULL);
}


/**********************************************
 *
 * Type-system functions
 *
 **********************************************/


Type* CancelledErr_initType(void)
{
  if (CancelledErr_classType == NULL)
  {
    Type* baseType = Err_initType();
    VTable* vtable = CancelledErr_initVTable();
    Type* type = Type_new("CancelledErr", baseType, vtable, sizeof(CancelledErr), sizeof(CancelledErrVTable));
    Type_registerCtorImplementation(type, "make", CancelledErr_make_0, 0);
    Type_registerCtorImplementation(type, "make", CancelledErr_make_1, 1);
    Type_registerCtorImplementation(type, "make", CancelledErr_make, 2);
    CancelledErr_classType = type;
    CancelledErr_initStatic();
  }
  return CancelledErr_classType;
}


VTable* CancelledErr_initVTable(void)
{
  if (CancelledErr_classVTable == NULL)
  {
    VTable* vtable = memoryAlloc(sizeof(CancelledErrVTable));
    memcpy(vtable, Err_initVTable(), sizeof(ErrVTable));
    CancelledErr_classVTable = vtable;
  }
  return CancelledErr_classVTable;
}


void CancelledErr_initStatic(void)
{
}
